// utils/requestPromise.js
export default function requestPromise(config) {
  const mytoken = uni.getStorageSync('token');

  const header = mytoken
    ? { "Authorization": mytoken }
    : {
        "Authorization":
          "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6ImJkZGQ0NGJlLWRlNGMtNDJhNC04ZmZiLWY3ODFmN2ZlMjU2NiIsInVzZXJuYW1lIjoiYWRtaW4ifQ.PHernMSzs-Z3jnP_crHoU33cMEBgA5tRAfzei9Q3HlVMn2q6LT6q2ngfLO4kkTkvM7RSKUZgDgLw6q00hzuooA"
      };

  return new Promise((resolve, reject) => {
    const requestConfig = {
      url: '',
      method: config.method || 'GET',
      data: config.data || {},
      params: config.params || {},
      header,
      success: (res) => resolve(res),
      fail: (err) => reject(err)
    };

    // H5
    // #ifdef H5
    requestConfig.url = "http://localhost:9999/api" + config.url;
    uni.request(requestConfig);
    // #endif

    // 微信小程序
    // #ifdef MP-WEIXIN
    requestConfig.url = "http://localhost:8080" + config.url;
    uni.request(requestConfig);
    // #endif
  });
}
